<?php
declare(strict_types=1);

namespace App\Controller\Api\V1\PositionSupervise;

use App\Controller\Api\BaseController;
use App\Repositories\PointPositionRepository;
use App\Repositories\PositionSupervisorApplyRepository;
use App\Repositories\UserHasPositionRepository;
use App\Request\StoreSupervisorApplyRequest;
use App\Services\UserService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

class SupervisorController extends BaseController{
    /**
     * @Inject
     * @var UserService
     */

    protected $user_service;

    /**
     * @param StoreSupervisorApplyRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function apply(StoreSupervisorApplyRequest $request){
        $user = $this->request->getAttribute('user');
        $params = $request->all();
        DB::beginTransaction();
        try {
            $result = $this->user_service->storeInfoBasic($user,$params);
            if(!$result['success']) {
                throw new \Exception($result['msg']);
            }
            $apply_result = PositionSupervisorApplyRepository::getInstance()->store([
                'user_id' => $user->id,
                'status' => 0
            ]);
            if(!$apply_result['success']){
                throw new \Exception('申请失败，稍后重试');
            }
            DB::commit();
        }catch(\Throwable $e){
            DB::rollBack();
            return $this->response->error(10050,$e->getMessage());
        }
        return $this->response->success([
            'id' => $apply_result['data']->id
        ]);
    }

    public function positions(){
        $user = $this->request->getAttribute('user');
        $category_id = $this->request->input('category_id',0);
        $district_id = $this->request->input('district_id',0);
        $name = $this->request->input('name','');
        $search=[];
        $search['category_id']=[
            'field' => 'category_id',
            'value' => $category_id
        ];
        $search['district_id']=[
            'field' => 'district_id',
            'value' => $district_id
        ];
        if($name!=''){
            $search['name']=[
                'field' => 'name',
                'value' => $name,
                'method' => 'like'
            ];
        }
        $orderBy= [
            [
                'field' => 'id',
                'sort' => 'asc'
            ]
        ];
        $column=[
            '*'
        ];
        $datas = PointPositionRepository::getInstance()->getList($column,$search,false,$orderBy);
        return $this->response->success($datas);
    }

    public function myPosition(){
        $user = $this->request->getAttribute('user');
        $category_id = $this->request->input('category_id',0);
        $district_id = $this->request->input('district_id',0);
        $search=[];
        $search['user_id']=[
            'field' => 'user_id',
            'value' => $user->id
        ];
        $search['category_id']=[
            'field' => 'category_id',
            'value' => $category_id
        ];
        $search['district_id']=[
            'field' => 'district_id',
            'value' => $district_id
        ];
        $join = [
            'point_position' => [
                'on' => ['user_has_position.position_id','=','point_position.id']
            ]
        ];
        $orderBy= [
            [
                'field' => 'user_has_position.id',
                'sort' => 'desc'
            ]
        ];
        $column=[
            'point_position.*'
        ];
        $datas = UserHasPositionRepository::getInstance()->getList($column,$search,false,$orderBy,$join);
        return $this->response->success($datas);
    }
}